/**
 * history 已结账单
 */
Jet().$package('app.history', function(J){
	var $=J.dom.id,
		$D=J.dom,
		$E=J.event,
		$H=J.http,
		$U=J.ui,
		$tool=J.tool;
	
	//已结账单列表
	this.listHistory = function(page){
		$D.html($('data_history'), '<tr><td colspan="4"><div class="loading"></div></td></tr>');
		var uri = '/ajax.php?d=bill&c=CheckoutProxy&f=history&limit=20&page='+page+'&group_id='+$('sel_group').value;
		$tool.get(uri,function(response){
			if(response.result==0){
				$tool.alertMsg(response.data);
				$D.empty($('data_history'));
				return;
			}
			var memberKV = app.memberKV,
				sHtml = '';
			J.each(response.data.list,function(key,row){
				sHtml += '<tr>';
				sHtml += '<td>'+$tool.datetime(row.c_close_time)+'</td>';
				sHtml += '<td>'+row.c_total+'</td>';
				sHtml += '<td>'+memberKV[row.c_creator]+'</td>';
				var summaryLink = '<a href="javascript:app.history.summary('+row.c_id+');">汇总</a>',
					recordLink = '<a href="javascript:app.history.openListBill('+row.c_id+');">记账列表</a>';
				sHtml += '<td>'+summaryLink+'&nbsp;&nbsp;'+recordLink+'</td>';
				sHtml += '</tr>';
			});
			if(sHtml!='') $D.html($('data_history'), sHtml);
			else $D.html($('data_history'), '<tr><td colspan="4">暂无数据~</td></tr>');
			$U.pager({ 'pageNode': $('page_history'), 
				'pageCurrent': page, 
				'pageCount': 20, 
				'count': response.data.count, 
				'callback': this.listHistory
			});			
		});				
	};
	
	//账单汇总
	this.summary = function(checkout_id) {
		$D.html($('summary_data'), '<tr><td colspan="4"><div class="loading"></div></td></tr>');
		var uri = '/ajax.php?d=bill&c=CheckoutProxy&f=summary&checkout_id='+checkout_id;
		$tool.get(uri,function(response){
			var memberKV = app.memberKV,
				checkoutRow = response.data.checkout,
				coUserList = response.data.co_users,
				sHtml = '';
			J.each(coUserList, function(key,row){
				sHtml += '<tr>';
				sHtml += '<td>'+memberKV[row.u_id]+'</td>';
				sHtml += '<td>'+row.cu_person_pay+'</td>';
				sHtml += '<td>'+row.cu_person_spend+'</td>';
				sHtml += '<td>'+row.cu_balance+'</td>';
				sHtml += '</tr>';
			});
			$D.html($('summary_data'), sHtml);
			var ddNodes = $D.mini('#summary_dialog dd');
			$D.text(ddNodes[0], memberKV[checkoutRow.c_creator]);
			$D.text(ddNodes[1], checkoutRow.c_total);
			$D.text(ddNodes[2], $tool.datetime(checkoutRow.c_close_time));
		});		
		J.ui.dialog({
			'divNode':$('summary_dialog'), 
			'title':'账单汇总', 
			'width':'600px',
			'height':'auto',
			'isDrag':false,
			'isLayer':true
		});		
	};
	
	//记账列表
	this.openListBill = function(checkout_id) {
		J.ui.dialog({
			'divNode':$('history_bill_list'), 
			'title':'记账列表', 
			'width':'600px',
			'height':'400px',
			'isDrag':false,
			'isLayer':true
		});
		this.listBill(1, checkout_id);
	};
	this.listBill = function(page,checkout_id) {
		$D.html($('history_bill_data'), '<tr><td colspan="5"><div class="loading"></div></td></tr>');
		var uri = '/ajax.php?d=bill&c=BillProxy&f=listing&limit=20&page='+page+'&checkout_id='+checkout_id;
		$tool.get(uri,function(response){
			var data = response.data;
			var memberKV = app.memberKV,
				sHtml = '';
			J.each(data.list,function(key,row){
				sHtml += '<tr>';
				sHtml += '<td>'+$tool.datetime(row.b_create_time)+'</td>';
				sHtml += '<td>'+row.b_note+'</td>';
				sHtml += '<td>'+row.b_amount+'</td>';
				sHtml += '<td>'+memberKV[row.b_creator]+'</td>';
				sHtml += '<td><a href="javascript:app.current.detail('+row.b_id+');">详细</a></td>';
				sHtml += '</tr>';
			});
			$D.html($('history_bill_data'), sHtml);
			$U.pager({ 'pageNode': $('history_bill_page'), 
				'pageCurrent': page, 
				'pageCount': 20, 
				'count': data.count, 
				'callback': app.history.listBill,
				'parameter': [checkout_id]
			});			
		});		
	};
	
});
